package com.csiec.clock.model;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.csiec.clock.bean.clock.BackData;
import com.csiec.clock.bean.UserInfo;
import com.csiec.clock.bean.clock.RealData;
import com.csiec.clock.bean.people.ResultCode;
import com.csiec.clock.callback.ClockCallBack;
import com.csiec.clock.callback.ClockInfoCallBack;
import com.csiec.clock.utils.Constant;
import com.csiec.clock.utils.JsonUtils;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ClockModel {

    private final String www = Constant.www;
    private final String getClockInUrl = "/student/iclocklist";
    private final String getIClockDataUrl = "/student/iclockdata";
    private final String goClockUrl = "/clockin/interface";

    /**
     * //获取用户自建打卡列表
     *
     * @param callBack
     */
    public void getClock(final ClockCallBack callBack) {
        new Thread(() -> {
            try {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .build();
                Request request = new Request.Builder()
                        .addHeader("token", UserInfo.getUserInfo().getToken())
                        .url(www + getClockInUrl)
                        .post(body)
                        .build();//创建一个Request对象

                Response response = client.newCall(request).execute();//发送请求获取返回数据
                String responseData = response.body().string();//处理返回的数据

                BackData backData = JSONObject.parseObject(responseData, BackData.class);

                callBack.onSuccess(backData);

            } catch (Exception e) {
                callBack.onFailed();
                e.printStackTrace();
            }
        }).start();

    }


    /**
     * //获取用户自建打卡参与人员和打卡比例
     *
     * @param id       打卡id
     * @param data     查询时间
     * @param callBack 回调函数
     */
    public void getIClockDataUrl(final String id, final String data, final ClockInfoCallBack callBack) {
        new Thread(() -> {
            try {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .add("id", id)
                        .add("date", data)
                        .build();
                Request request = new Request.Builder()
                        .addHeader("token", UserInfo.getUserInfo().getToken())
                        .url(www + getIClockDataUrl)
                        .post(body)
                        .build();//创建一个Request对象

                Response response = client.newCall(request).execute();//发送请求获取返回数据
                String responseData = response.body().string();//处理返回的数据
                ResultCode backData = JSONObject.parseObject(responseData, ResultCode.class);
                callBack.onSuccess(backData);

            } catch (Exception e) {
                callBack.onFailed();
                e.printStackTrace();
            }
        }).start();

    }


    /**
     * 打卡
     *
     * @param id        打卡id
     * @param res       输入内容
     * @param latitude  纬度
     * @param longitude 经度
     * @param callBack  回调函数
     */
    public void goClock(final String id, final String res, final String latitude, final String longitude, final ClockInfoCallBack callBack) {
        new Thread(() -> {
            try {
                OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                RequestBody body = new FormBody.Builder()
                        .add("base64", UserInfo.getUserInfo().getBase64())
                        .add("id", id)
                        .add("res", res)
                        .add("latitude", latitude)
                        .add("longitude", longitude)
                        .build();
                Request request = new Request.Builder()
                        .addHeader("token", UserInfo.getUserInfo().getToken())
                        .url(www + goClockUrl)
                        .post(body)
                        .build();//创建一个Request对象

                Response response = client.newCall(request).execute();//发送请求获取返回数据
                String responseData = response.body().string();//处理返回的数据
                ResultCode backData = JSONObject.parseObject(responseData, ResultCode.class);

                callBack.onSuccess(backData);

            } catch (Exception e) {
                callBack.onFailed();
                e.printStackTrace();
            }
        }).start();

    }


}
